package net.cathackers.devel.scmp.xmpp;

import net.cathackers.devel.scmp.xmpp.exceptions.UnknownPacket;

import org.dom4j.Document;
/**
 * Parsing incoming DOM objects into suitable Packet objects
 *
 */
public class PacketRegistery {
    /**
     * create Packet object from incoming DOM objects
     * first we get the name of root element.
     * if the name was "iq" we will create IQPacket object and so on.
     * @see IQPacket
     * @param doc
     * @return Packet
     * @see Packet
     * @throws UnknownPacket if the name wasn't "iq" or "message" or "presence"
     */
    public static Packet createPacket(Document doc) throws UnknownPacket{
	String rootElementName = doc.getRootElement().getName();
	Packet ret = null;
	if (rootElementName.equalsIgnoreCase("iq")) {
	    ret = new IQPacket(doc);
	}else if (rootElementName.equalsIgnoreCase("message")) {
	    ret = new MessagePacket(doc);
	}else if (rootElementName.equalsIgnoreCase("presence")) {
	    ret = new PresencePacket(doc);
	}else {
	    throw new UnknownPacket("Unknown packet of type " + rootElementName);
	}
	return ret;
    }
}
